﻿using SlamDunk.CMS.DAL;
using SlamDunk.CMS.Model.DataModel;
using System;
using System.Collections.Generic;

namespace SlamDunk.CMS.BLL
{
    public class PhotoAlbumBll
    {
        #region 01.实例化数据层
        /// <summary>
        /// 实例化数据层
        /// </summary>
        private readonly PhotoAlbumDal CurrentDal = new PhotoAlbumDal();
        #endregion

        #region 02.添加
        /// <summary>
        /// 添加一个实体对象
        /// </summary>
        /// <param name="photoAlbum">构造好要添加的实体对象</param>
        /// <returns>添加完毕后的实体对象</returns>
        public PhotoAlbum Add(PhotoAlbum photoAlbum)
        {
            return CurrentDal.Add(photoAlbum);
        }
        #endregion

        #region 03.删除（物理删除）
        /// <summary>
        /// 删除一个实体对象
        /// </summary>
        /// <param name="id">要删除的实体对象Id</param>
        /// <returns>删除结果</returns>
        public bool PhysicalDelete(int id)
        {
            return CurrentDal.PhysicalDelete(id) > 0;
        }
        #endregion

        #region 05.修改
        /// <summary>
        /// 修改一个实体对象
        /// </summary>
        /// <param name="photoAlbum">构造好的要修改的实体对象</param>
        /// <returns>修改结果</returns>
        public bool Update(PhotoAlbum photoAlbum)
        {
            return CurrentDal.Update(photoAlbum) > 0;
        }
        #endregion

        #region 06.根据主键获取单个实体对象
        /// <summary>
        /// 根据主键获取单个实体对象
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>单个实体对象</returns>
        public PhotoAlbum GetModel(int id)
        {
            return CurrentDal.GetModel(id);
        }
        #endregion

        #region 07.获取实体对象集合
        /// <summary>
        /// 获取所有实体对象
        /// </summary>
        /// <returns>实体对象集合</returns>
        public List<PhotoAlbum> GetModelList()
        {
            return CurrentDal.GetModelList();
        }
        #endregion

        #region 08.获取总记录条数
        /// <summary>
        /// 返回总记录条数
        /// </summary>
        /// <returns>总记录条数</returns>
        public int GetCount()
        {
            return CurrentDal.GetCount();
        }
        #endregion

        #region 09.分页获取实体对象集合
        /// <summary>
        /// 分页获取实体对象集合
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="rowCount">总记录条数</param>
        /// <returns>实体对象集合</returns>
        public List<PhotoAlbum> GetModelList(int pageIndex, int pageSize, out int pageCount, out int rowCount)
        {
            //1.计算总记录条数与总页数
            rowCount = this.GetCount();
            pageCount = (int)Math.Ceiling((rowCount * 1.0 / pageSize));
            //2.页索引处理
            pageIndex = pageIndex < 1 ? 1 : pageIndex;
            pageIndex = pageIndex >= pageCount ? pageCount : pageIndex;
            //3.计算起始行与终止行
            int start = (pageIndex - 1) * pageSize + 1;
            int end = pageIndex * pageSize;
            //4.调用数据层返回相应实体集合
            return CurrentDal.GetModelList(start, end);
        }
        #endregion


        #region 03.根据页容量获取总页数
        /// <summary>
        /// 根据页容量获取总页数
        /// </summary>
        /// <param name="pageSize">页容量</param>
        /// <returns>总页数</returns>
        public int GetPageCount(int pageSize)
        {
            int rowCount = this.GetCount();
            int pageCount = (int)Math.Ceiling((rowCount * 1.0 / pageSize));
            return pageCount;
        }
        #endregion

        #region 04.分页获取实体对象集合
        /// <summary>
        /// 分页获取实体对象集合
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="rowCount">总记录条数</param>
        /// <returns>实体对象集合</returns>
        public List<PhotoAlbum> GetModelList(int pageIndex, int pageSize, out int rowCount)
        {
            //1.计算总记录条数与总页数
            rowCount = this.GetCount();
            int pageCount = (int)Math.Ceiling((rowCount * 1.0 / pageSize));
            //2.页索引处理
            pageIndex = pageIndex < 1 ? 1 : pageIndex;
            pageIndex = pageIndex >= pageCount ? pageCount : pageIndex;
            //3.计算起始行与终止行
            int start = (pageIndex - 1) * pageSize + 1;
            int end = pageIndex * pageSize;
            //4.调用数据层返回相应实体集合
            return this.CurrentDal.GetModelList(start, end);
        }
        #endregion
    }
}
